package event.customtag;

import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import bean.Person;

public class PersonListTag extends TagSupport {

    private static final long serialVersionUID = -4854604666993191018L;
    private List<Person> persons;
    private boolean boldHeader;
    private boolean displayHeader;
    private String title;

    @Override
    public int doStartTag() throws JspException {

        try {
            // Get the writer object for output.
            JspWriter out = pageContext.getOut();

            // Perform substr operation on string.
            out.println(displayTitle());
            out.println(displayPersonsTable());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }

    private String displayPersonsTable() {
        StringBuilder table = new StringBuilder("<table border='1' width ='300' cellpadding ='3'>");
        table.append("<tr>");
        if (displayHeader) {
            if (boldHeader) {
                table.append("<td><b>SELECT</b></td>" + "<td><b>ID</b></td>" + "<td><b>LAST NAME</b></td>" + "<td><b>FIRST NAME</b></td>" + "<td><b>AGE</b></td>");
            } else {
                table.append("<td>SELECT</td>" + "<td>ID</td>" + "<td>LAST NAME</td>" + "<td>FIRST NAME</td>" + "<td>AGE</td>");
            }
        }
        for (Person pers : persons) {
            table.append("<tr>");
            table.append("<td>").append("<input type='checkbox'").append("onclick ='enableButton()'").append("id='records'").append("name ='id'").append("value='").append(pers.getId()).append("'")
                    .append(">").append("</td>");
            table.append("<td>").append(pers.getId()).append("</td>");
            table.append("<td>").append(pers.getLastName()).append("</td>");
            table.append("<td>").append(pers.getFirstName()).append("</td>");
            table.append("<td>").append(pers.getAge()).append("</td>");
            table.append("</tr>");
        }
        return table.toString();
    }

    private String displayTitle() {
        StringBuilder title = new StringBuilder();
        if (title != null) {
            title.append("<h2>").append(getTitle()).append("</h2>");
        }
        return title.toString();
    }

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    public boolean isBoldHeader() {
        return boldHeader;
    }

    public void setBoldHeader(boolean boldHeader) {
        this.boldHeader = boldHeader;
    }

    public boolean isDisplayHeader() {
        return displayHeader;
    }

    public void setDisplayHeader(boolean displayHeader) {
        this.displayHeader = displayHeader;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
